<?php

namespace application\controller\cpservice;

use application\controller\cpservice\ServiceController;
use application\service\DataService;
use application\model\ImageModel;
use core\system\HttpRequest;

class DataController extends ServiceController {

    private $dataService;
    private $imageModel;

    public function __construct() {
        parent::__construct();
        $this->dataService = new DataService();
        $this->imageModel = new ImageModel();
    }

    /**
     * @module
     * @comment(Lấy thông tin bảng theo className)
     * @mapping(value=cpservice/database/get.json)
     */
    public function get() {
        $table = $this->dataService->getTableInfo(HttpRequest::POST("className"));
        if ($table == null) {
            $this->view->response(false, "Không tìm thấy bảng yêu cầu " + HttpRequest::POST("className"));
        }
        $this->view->response(true, "Thông tin bảng", $table);
    }

    /**
     * @module     
     * @comment(Xóa dữ liệu)
     * @mapping(value=cpservice/database/clear.json)
     */
    public function clear() {
        $this->imageModel->clear(HttpRequest::POST("tableName"));
        $this->view->response(true, "Bảng " . HttpRequest::POST("tableName") . " đã được clear");
    }

    /**
     * @module
     * @comment(Xóa bảng)
     * @mapping(value=cpservice/database/remove.json)
     */
    public function remove() {
        $this->imageModel->dropTable(HttpRequest::POST("tableName"));
        $this->view->response(true, "Bảng " . HttpRequest::POST("tableName") . " đã được drop");
    }

    /**
     * @module
     * @comment(Tạo bảng theo className)
     * @mapping(value=cpservice/database/create.json)
     */
    public function create() {
        $resp = $this->dataService->createTable(HttpRequest::POST("className"));
        return $resp->resultData();
    }

    /**
     * @module
     * @comment(Tạo database)
     * @mapping(value=cpservice/database/createdatabase.json)
     */
    public function createDatabase() {
        $resp = $this->dataService->createDatabase();
        return $resp->resultData();
    }

}
